8592. Simple function

 

Implement a function f(x) = x + sin(x).

 

Input. One real value x.

 

Output. Print the value of f(x) with 4 decimal digits.

 

Sample input 1

Sample output 1

1.1234

2.0250

 

 

Sample input 2

Sample output 2

-3.1415

-3.1416

 

 

SOLUTION

functions

 

Algorithm analysis

Implement a function f(x).

 

Algorithm realization

Implement a function f.

 

double f(double x)

{

  return x + sin(x);

}

 

The main part of the program. Read the input value of x.

 

scanf("%lf",&x);

 

Compute and print the value of the function f(x).

 

y = f(x);

printf("%.4lf\n",y);

 

Java realization

 

import java.util.*;

 

public class Main

{

  static double f(double x)

  {

    return x + Math.sin(x);

  }

 

  public static void main(String[] args)

  {

    Scanner con = new Scanner(System.in);

    double x = con.nextDouble();

    double y = f(x);

    System.out.println(y);   

    con.close();

  }

}  

 

Python realization

Implement a function f.

 

import math

 

def f(x):

  return x + math.sin(x)

 

The main part of the program. Read the input value of x.

 

x = float(input())

 

Compute and print the value of the function f(x).

 

print(f(x))